var express = require('express');
var router = express.Router();
let mongoose = require('mongoose');
//查询所有课程信息
router.get('/',async function(req, res){
re= await   mongoose.model('courseModel').find({})
 res.send({
    mes:'成功',
    code:200,
    data:re
})
})
//删除
router.delete('/:_id',async function(req, res) {
//    console.log(req.params);
    await mongoose.model('courseModel').deleteMany({
        _id:mongoose.Types.ObjectId(req.params._id)
    })
    res.send({
        mes:"删除成功"
    })
})
//添加
router.post('/:name/:classHour/:credit',async function (req, res) {
    console.log(req.params);
    await mongoose.model('courseModel').create({
        name:req.params.name,
        classHour:req.params.classHour,
        credit:req.params.credit
    })
    res.send({
        mes:"添加成功"
    })
})
//修改
router.put(`/:_id/:name/:classHour/:credit`,async function (req, res) {
    console.log(req.params);
    await mongoose.model('courseModel').updateMany({
     _id:req.params._id
    },{
      name:req.params.name,
      classHour:req.params.classHour,
      credit:req.params.credit
    })
    res.send({
        mes:"添加成功"
    })
})
module.exports = router;
